package com.sunfield.baseframe.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.sunfield.baseframe.R;
import com.sunfield.baseframe.util.MLog;

/**
 * 仿windows loading动画
 * Created by zhangml on 2016/9/12 0012.
 */
public class WinLoadingView extends View {
    private final String TAG = "LOADING";

    private Paint mPaint;
    private Path mPath;
    private PathMeasure mPathMeasure;
    private int mWidth, mHeight;
    private ValueAnimator valueAnimator;
    //用这个来接受ValueAnimator的返回值，代表整个动画的进度
    private float t;

    private Paint centerPaint;

    public WinLoadingView(Context context) {
        super(context);
    }

    public WinLoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
//        valueAnimator.start();
        MLog.d(TAG, "start");
    }

    public void startAnimator(){
        valueAnimator.start();
    }
    public void stopAnimator(){
        valueAnimator.cancel();
    }
    public boolean isAnimatorRunning(){
        return valueAnimator.isRunning();
    }

    private void init() {

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(20);
        mPaint.setColor(getResources().getColor(R.color.loading_point));
        //设置画笔为园笔
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        //抗锯齿
        mPaint.setAntiAlias(true);

        centerPaint = new Paint();
        centerPaint.setStyle(Paint.Style.STROKE);
        centerPaint.setStrokeWidth(20);
        centerPaint.setColor(Color.WHITE);
        //设置画笔为园笔
        centerPaint.setStrokeCap(Paint.Cap.ROUND);
        //抗锯齿
        centerPaint.setAntiAlias(true);

        mPath = new Path();
        RectF rect = new RectF(-100, -100, 100, 100);
        mPath.addArc(rect, -90, 359.9f);

        mPathMeasure = new PathMeasure(mPath, false);

        valueAnimator = ValueAnimator.ofFloat(0f, 1f).setDuration(3000);
        valueAnimator.setRepeatCount(-1);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                t = (float) animation.getAnimatedValue();
                postInvalidate();
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mWidth / 2, mHeight / 2);
        Path dst = new Path();
        if (t >= 0.95) {
//            canvas.drawPoint(0, -150, mPaint);
            Path dstTop = new Path();
            dstTop.lineTo(0, 0);
            mPathMeasure.getSegment(-1, 1, dstTop, true);
            canvas.drawPath(dstTop, mPaint);
        }
        dst.lineTo(0, 0);
        int num = (int) (t / 0.05);
        float s, y, x;
        switch (num) {
            default:
            case 3:
                x = t - 0.15f * (1 - t);
                s = mPathMeasure.getLength();
                y = -s * x * x + 2 * s * x;
                mPathMeasure.getSegment(y, y + 1, dst, true);
            case 2:
                x = t - 0.10f * (1 - t);
                s = mPathMeasure.getLength();
                y = -s * x * x + 2 * s * x;
                mPathMeasure.getSegment(y, y + 1, dst, true);
            case 1:
                x = t - 0.05f * (1 - t);
                s = mPathMeasure.getLength();
                y = -s * x * x + 2 * s * x;
                mPathMeasure.getSegment(y, y + 1, dst, true);
            case 0:
                x = t;
                s = mPathMeasure.getLength();
                y = -s * x * x + 2 * s * x;
                mPathMeasure.getSegment(y, y + 1, dst, true);
                break;
        }
        canvas.drawPath(dst, mPaint);
        canvas.drawPoint(0, 0, centerPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        MLog.d(TAG, "mWidth = " + mWidth);
        MLog.d(TAG, "mHeight = " + mHeight);
    }
}
